package com.buildings.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.bean.BaseBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.Base64Test;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import com.buildings.util.SPUtil;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class ModifyPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText etPassword;
    private EditText etPasswordAgain;
    private EditText etPasswordOld;
    private Button btnCommit;
    private Dialog myDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        initView();
    }

    private void initView(){
        findViewById(R.id.iv_back).setOnClickListener(this);
        etPassword = findViewById(R.id.et_password);
        etPasswordAgain = findViewById(R.id.et_password_again);
        etPasswordOld = findViewById(R.id.et_password_old);
        btnCommit = findViewById(R.id.btn_commit);
        btnCommit.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("修改密码");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_commit:

                if(TextUtils.isEmpty(etPasswordOld.getText().toString())){
                    CustomToast.showToast(activity,"请输入原密码");
                    return;
                }
                if(TextUtils.isEmpty(etPassword.getText().toString())){
                    CustomToast.showToast(activity,"请输入新密码");
                    return;
                }
                if(TextUtils.isEmpty(etPasswordAgain.getText().toString())){
                    CustomToast.showToast(activity,"请输入确认密码");
                    return;
                }
                if(!RegisterActivity.checkPassword(etPassword.getText().toString())){
                    CustomToast.showToast(activity,"密码必须包含数字和字母");
                    return;
                }
                if(!TextUtils.equals(etPassword.getText().toString(),etPasswordAgain.getText().toString())){
                    CustomToast.showToast(activity,"两次密码输入不一致");
                    return;
                }
                myDialog = DialogUtils.showRefreshDialog(activity);

                //调用修改密码接口
                String userId = (String) SPUtil.get("userId","");
                modify(userId,etPasswordOld.getText().toString(),etPassword.getText().toString());
                break;
        }
    }

    private void modify(String userId,String oldPwd,String newPwd) {
        String encryptOldPwd="";
        String encryptNewPwd="";
        try {
            encryptOldPwd = LoginActivity.encryptDataStr(oldPwd.getBytes(),LoginActivity.getPublicKey(Base64Test.decode(Constant.PUBLIC_KEY)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            encryptNewPwd = LoginActivity.encryptDataStr(newPwd.getBytes(),LoginActivity.getPublicKey(Base64Test.decode(Constant.PUBLIC_KEY)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        JsonBuilder jsonBuilder = new JsonBuilder(this);
        jsonBuilder.put("userId", userId);
        jsonBuilder.put("oldPwd", encryptOldPwd);
        jsonBuilder.put("newPwd", encryptNewPwd);

        MyHttpClient
                .newBuilder()
                .post()
                .url(Api.UPDATE_PWD)
                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean bean) {
                        super.onSuccess(bean);
                        if (myDialog != null)
                            myDialog.dismiss();

                        if(bean.getCode() == 200){
                            CustomToast.showToast(activity, "修改完成，请重新登录");
                            SPUtil.clear();
                            Intent intent = new Intent(activity,LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                        }else {
                            if(!TextUtils.isEmpty(bean.getMessage())){
                                CustomToast.showToast(activity, bean.getMessage());
                            }else {
                                CustomToast.showToast(activity, "密码修改失败");
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");

                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");

                    }
                });

    }

}
